﻿
namespace Patterns4Net.ArchitectureExplorer.Views.Utils
{
    using System;
    using System.Linq;
    using System.Collections;
    using System.Globalization;
    using System.Windows;
    using System.Windows.Data;
    using Core;

    public class EmptyOrNullToVisibilityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null)
            {
                return Visibility.Collapsed;
            }

            if (value is IEnumerable)
            {
                if (((IEnumerable)value).OfType<object>().IsEmpty())
                {
                    return Visibility.Collapsed;
                }
            }

            return Visibility.Visible;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            var visibility = (Visibility)value;
            if (visibility == Visibility.Collapsed)
            {
                return null;
            }

            return new object();
        }
    }
}
